﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using miSuperEngine;

namespace miSuperEngine
{
    public class Input
    {
        KeyboardState oldKeyState;
        KeyboardState currentKeyState;
        GamePadState[] currentGamePadState=new GamePadState[4];
        GamePadState[] oldGamePadState=new GamePadState[4];
        public Input()
        {
            oldKeyState = Keyboard.GetState();
            currentKeyState = oldKeyState;
            for (int i=0;i<4;i++)
                currentGamePadState[i]=GamePad.GetState((PlayerIndex)i);
        }
        public bool isNewKeyPress(Keys key)
        {
            return (currentKeyState.IsKeyDown(key) && !oldKeyState.IsKeyDown(key));
        }
        public bool isNewKeyUp(Keys key)
        {
            return (!currentKeyState.IsKeyDown(key) && oldKeyState.IsKeyDown(key));
        }
        public void update(GameTime gt)
        {
            oldKeyState = currentKeyState;
            currentKeyState = Keyboard.GetState();
            for (int i = 0; i < 4; i++)
            {
                oldGamePadState[i] = currentGamePadState[i];
                currentGamePadState[i] = GamePad.GetState((PlayerIndex)i);
            }
        }
        public bool isNewButtonDown(Buttons button,PlayerIndex? pi, out PlayerIndex outIndex)
        {
            if (pi != null)
            {
                outIndex = (PlayerIndex) pi;
                return (currentGamePadState[(int)pi].IsButtonDown(button) && oldGamePadState[(int)pi].IsButtonUp(button));
            }
            else
            {
                return (isNewButtonDown(button, PlayerIndex.One, out outIndex) ||
                isNewButtonDown(button, PlayerIndex.Two, out outIndex) ||
                isNewButtonDown(button, PlayerIndex.Three, out  outIndex) ||
                isNewButtonDown(button, PlayerIndex.Four, out outIndex));
            }
        }
    }

}
